package com.easylinkin.linkappapi.reportCenter.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.reportCenter.entity.Report;
import com.easylinkin.linkappapi.reportCenter.entity.dto.ReportDetailDTO;
import com.easylinkin.linkappapi.reportCenter.entity.vo.ReportDeviceVo;
import com.easylinkin.linkappapi.reportCenter.service.DeviceReportService;
import com.easylinkin.linkappapi.reportCenter.timer.DeviceReportTimer;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description 设备运行报告
 * @Author kan yuanfeng
 * @Date 2023/7/13 14:10
 */
@RestController
@RequestMapping("deviceReport")
@Api(tags = "设备运行报告")
public class DeviceReportController {

    @Autowired
    private DeviceReportService deviceReportService;

    @Autowired
    private DeviceReportTimer deviceReportTimer;

    @Resource
    private LinkappUserContextProducer userContextProducer;

    /**
     * @Description: 根据条件，分页(不分页)查询报表
     * @author kan yuanfeng
     * @date 2023/07/14 11:42
     */
    @PostMapping("list")
    @ApiOperation("根据条件，分页(不分页)查询报表")
    public RestMessage findReportList(@RequestBody RequestModel<Report> requestModel){
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<Report> record = deviceReportService.findReportList(requestModel);
        return RestBuilders.successBuilder().data(record).build();
    }

    /**
     * @Description: 运行报告设备统计
     * @author kan yuanfeng
     * @date 2023/07/14 11:42
     */
    @GetMapping("device/{reportId}")
    @ApiOperation("运行报告设备统计")
    public RestMessage findDevice(@PathVariable("reportId")Integer reportId){
        Assert.notNull(reportId, "报告id不能为空");
        List<ReportDeviceVo> record = deviceReportService.findDevice(reportId);
        return RestBuilders.successBuilder().data(record).build();
    }

    /**
     * @Description: 运行报告告警统计
     * @author kan yuanfeng
     * @date 2023/07/14 11:42
     */
    @GetMapping("alarm/{reportId}")
    @ApiOperation("运行报告告警统计")
    public RestMessage findAlarm(@PathVariable("reportId")Integer reportId){
        Assert.notNull(reportId, "报告id不能为空");
        Map<String,Object> record = deviceReportService.findAlarm(reportId);
        return RestBuilders.successBuilder().data(record).build();
    }

    /**
     * 绿色施工查询
     */
    @GetMapping("energy/{reportId}")
    @ApiOperation("绿色施工查询")
    public RestMessage findEnergy(@PathVariable("reportId")Integer reportId){
        Assert.notNull(reportId, "报告id不能为空");
        List<ReportDetailDTO> record = deviceReportService.findEnergy(reportId);
        return RestBuilders.successBuilder().data(record).build();
    }

    /**
     * 安全管理查询
     */
    @GetMapping("safe/{reportId}")
    @ApiOperation("安全管理查询")
    public RestMessage findSafe(@PathVariable("reportId")Integer reportId){
        Assert.notNull(reportId, "报告id不能为空");
        List<ReportDetailDTO> record = deviceReportService.findSafe(reportId);
        return RestBuilders.successBuilder().data(record).build();
    }

    /**
     * 大型作业设备查询
     */
    @GetMapping("largeDevice/{reportId}")
    @ApiOperation("大型作业设备查询")
    public RestMessage findLargeDevice(@PathVariable("reportId")Integer reportId){
        Assert.notNull(reportId, "报告id不能为空");
        List<ReportDetailDTO> record = deviceReportService.findLargeDevice(reportId);
        return RestBuilders.successBuilder().data(record).build();
    }

    /**
     * 生成某月的报告
     */
    @GetMapping("createReport")
    @ApiOperation("生成某月的报告")
    public RestMessage findLargeDevice(String time) throws ParseException {
        Date date = null;
        if (StringUtils.isNotBlank(time)){
            SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
            date = sdf.parse(time);
        }
        deviceReportTimer.createReport(userContextProducer.getTenantId(),date);
        return RestBuilders.successBuilder().build();
    }
}
